//
//  TypeWorkCollectionViewCell.m
//  WULIHH
//
//  Created by tang on 16/4/25.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "TypeWorkCollectionViewCell.h"
//#import "UIImageView+AFNetworking.h"
#import "UIImageView+WebCache.h"


@interface TypeWorkCollectionViewCell ()
{
    UIButton *FavButton;
    UIButton *downLoadButton;
    
    UIImageView *backImageView;
}

@end


@implementation TypeWorkCollectionViewCell



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        backImageView = [[UIImageView alloc] initWithFrame:self.bounds];
        [backImageView setContentMode:UIViewContentModeScaleAspectFill];
        [backImageView setOpaque:NO];
        [backImageView.layer setMasksToBounds:YES];
        [self addSubview:backImageView];
        
        [self initUI];
        
    }
    return self;
}



- (void)initUI
{
//    [self setBackgroundColor:[UIColor redColor]];
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height-30.0, CGRectGetWidth(self.bounds), 30.0)];
    [bottomView setBackgroundColor:[UIColor whiteColor]];
    [self addSubview:bottomView];
    [bottomView setUserInteractionEnabled:YES];
    
    UIImage *image = nil;
    
    image = [UIImage imageNamed:@"lookuseFrom.png"];
    FavButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, bottomView.bounds.size.width/2.0, 30.0)];
    [FavButton setBackgroundImage:image forState:UIControlStateNormal];
    [FavButton addTarget:self action:@selector(pushUseImage:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:FavButton];
    
    
    image = [UIImage imageNamed:@"weishoucang.png"];
    downLoadButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(FavButton.frame), 0, bottomView.bounds.size.width/2.0, 30.0)];
    [downLoadButton setBackgroundImage:image forState:UIControlStateNormal];
    
    image = [UIImage imageNamed:@"yishoucang.png"];
    [downLoadButton setBackgroundImage:image forState:UIControlStateSelected];
    [bottomView addSubview:downLoadButton];

}


- (void)setTag:(NSInteger)tag
{
    [downLoadButton setTag:tag];
    [FavButton setTag:tag];
}



- (void)setModelWithFavImage:(NSDictionary *)dic withIDArray:(NSArray *)array
{
//    [downLoadButton setTag:[[dic valueForKey:@"id"] intValue]];
//    NSLog(@"%@", dic);
//    NSLog(@"%@------ %@" ,array,  [dic valueForKey:@"id"]);
    
    
    if ([array containsObject:[NSString stringWithFormat:@"%@", [dic valueForKey:@"id"]]])
    {
        [downLoadButton setUserInteractionEnabled:NO];
        [downLoadButton setSelected:YES];
//        NSLog(@"yes");

    }
    else
    {
        [downLoadButton setSelected:NO];
        [downLoadButton setUserInteractionEnabled:YES];
        [downLoadButton addTarget:self action:@selector(downLoadImage:) forControlEvents:UIControlEventTouchUpInside];
//        [downLoadButton setUserInteractionEnabled:NO];

    }
    [backImageView sd_setImageWithURL:[NSURL URLWithString:[dic valueForKey:@"imageUrl"]]];

}




- (void)downLoadImage:(UIButton *)sender
{
    if (_MTypeWorkCollectionViewCellDelegate && [_MTypeWorkCollectionViewCellDelegate respondsToSelector:@selector(downLoadImage:)]) {
        [_MTypeWorkCollectionViewCellDelegate downLoadImage:[NSString stringWithFormat:@"%d", (int)sender.tag]];
        [downLoadButton setSelected:YES];
        [downLoadButton setUserInteractionEnabled:NO];
    }
}



//查看引用
- (void)pushUseImage:(UIButton *)sender
{
    if (_MTypeWorkCollectionViewCellDelegate && [_MTypeWorkCollectionViewCellDelegate respondsToSelector:@selector(pushUseIMageViewWithTag:)])
    {
        [_MTypeWorkCollectionViewCellDelegate pushUseIMageViewWithTag:sender.tag];
    }
}



@end
